package com.lxmes.base.service.impl;

import java.util.List;

import com.lxmes.base.mapper.NcFieldDictMapper;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.base.mapper.NcTechDetailMapper;
import com.lxmes.base.domain.NcTechDetail;
import com.lxmes.base.service.INcTechDetailService;
import javax.validation.Validator;

/**
 * 工艺明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-06
 */
@Service
public class NcTechDetailServiceImpl implements INcTechDetailService
{
    @Autowired
    private NcTechDetailMapper ncTechDetailMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询工艺明细
     *
     * @param id 工艺明细主键
     * @return 工艺明细
     */
    @Override
    public NcTechDetail selectNcTechDetailById(Long id)
    {
        return ncTechDetailMapper.selectNcTechDetailById(id);
    }

    /**
     * 查询工艺明细列表
     *
     * @param ncTechDetail 工艺明细
     * @return 工艺明细
     */
    @Override
    public List<NcTechDetail> selectNcTechDetailList(NcTechDetail ncTechDetail)
    {
        List<NcTechDetail> ncTechDetails = ncTechDetailMapper.selectNcTechDetailList(ncTechDetail);

        return ncTechDetails;
    }

    /**
     * 新增工艺明细
     *
     * @param ncTechDetail 工艺明细
     * @return 结果
     */
    @Override
    public int insertNcTechDetail(NcTechDetail ncTechDetail)
    {
        ncTechDetail.setCreateTime(DateUtils.getNowDate());
        return ncTechDetailMapper.insertNcTechDetail(ncTechDetail);
    }

    /**
     * 修改工艺明细
     *
     * @param ncTechDetail 工艺明细
     * @return 结果
     */
    @Override
    public int updateNcTechDetail(NcTechDetail ncTechDetail)
    {
        ncTechDetail.setUpdateTime(DateUtils.getNowDate());
        return ncTechDetailMapper.updateNcTechDetail(ncTechDetail);
    }

    /**
     * 批量删除工艺明细
     *
     * @param ids 需要删除的工艺明细主键
     * @return 结果
     */
    @Override
    public int deleteNcTechDetailByIds(Long[] ids)
    {
        return ncTechDetailMapper.deleteNcTechDetailByIds(ids);
    }

    /**
     * 删除工艺明细信息
     *
     * @param id 工艺明细主键
     * @return 结果
     */
    @Override
    public int deleteNcTechDetailById(Long id)
    {
        return ncTechDetailMapper.deleteNcTechDetailById(id);
    }
    @Override
    public String imporIteminfo(List<NcTechDetail> ncTechDetailList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncTechDetailList) || ncTechDetailList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcTechDetail ncTechDetail : ncTechDetailList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncTechDetail);
                ncTechDetail.setCreateBy(operName);
                ncTechDetail.setUpdateBy(operName);
                this.insertNcTechDetail(ncTechDetail);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
